package com.truthbean.log.jdk;

import com.truthbean.log.Logger;
import com.truthbean.log.util.DateTimeHelper;
import com.truthbean.log.util.MsgHelper;

import java.util.logging.Level;

/**
 * @author TruthBean
 * @since 0.0.1
 */
public class JdkImpl implements Logger {
    /**
     * jdk logger
     */
    private static java.util.logging.Logger log;

    /**
     * set global logger to level FINER
     */
    public JdkImpl() {
        java.util.logging.Logger.getGlobal().getParent().getHandlers()[0].setLevel(Level.FINER);
    }

    private String className;

    @Override
    public void info(String msg) {
        String prefix = DateTimeHelper.nowStr() + " " + className + " : ";
        log.fine("[info] " + prefix + msg);
    }

    @Override
    public void info(String msg, Object... param) {
        String prefix = DateTimeHelper.nowStr() + " " + className + " : ";
        log.fine(MsgHelper.format("[info] " + prefix + msg, param));
    }

    @Override
    public void debug(String msg) {
        String prefix = DateTimeHelper.nowStr() + " " + className + " : ";
        log.info("[debug] " + prefix + msg);
    }

    @Override
    public void debug(String msg, Object... param) {
        String prefix = DateTimeHelper.nowStr() + " " + className + " : ";
        log.info(MsgHelper.format("[debug] " + prefix + msg, param));
    }

    @Override
    public void error(String msg, Throwable e) {
        String prefix = DateTimeHelper.nowStr() + " " + className + " : ";
        log.log(Level.SEVERE, "[error] " + prefix + msg, e);
    }

    @Override
    public void setClass(Class<?> clazz) {
        setClassName(clazz.getName());
    }

    @Override
    public void setClassName(String className) {
        this.className = className;
        log = java.util.logging.Logger.getLogger(className);
        log.setLevel(Level.FINER);
    }
}
